import 'package:flutter/material.dart';

void main() => runApp(HomePage());

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
  Animation<int> _doubleAnimation;
  AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(vsync: this, duration: const Duration(seconds: 2))..forward();
    _doubleAnimation = IntTween(begin: 0xffffffff, end: 0x00000000).animate(_animationController)
      ..addListener(() {
        print(_doubleAnimation.value.toString());
        setState(() {
        });
      })

      /**
     * status 四种状态
     * 1. dismiss 初始状态
     * 2. forward 从头到尾状态
     * 3. reverse 从尾到头状态
     * 4. complete 完成状态
     */
      ..addStatusListener((status) {
        print('status == $status');
        if(status == AnimationStatus.completed) {
          _animationController.reverse();
        }
        if(status == AnimationStatus.dismissed){
          _animationController.forward();
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("animation1"),
        ),
        body: Center(
          // ??= 如果值为空才赋值
          child: Container(
            width: 100,
            height: 100,
            color: Color(_doubleAnimation.value.toInt()),
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    // 释放动画内存
    _animationController.dispose();
  }
}
